/**
 * Created by DukeCrushIt on 2015/8/4.
 */
//module game{
    class ShopItemRender extends eui.ItemRenderer{
        public constructor(){
            super();
            this.skinName = "pet.ShopItemRenderSkin";
        }

        private onBuy(evt:egret.TouchEvent){
            SoundMgr.getInstance().playEffect(SoundMgr.BTN);
            if( !this.data.canBuy){
                return;
            }
            if( this.data.byDiamond){
                RemindPanel.getInstance().showContent("是否花费"+this.data.disPrice+"钻石购买"+this.data.name,this,this.okBuy);
            }else{
                //RemindPanel.getInstance().showContent("是否花费"+this.data.hair+"毛发购买"+this.data.name,this,this.okBuy);
                this.okBuy();
            }
            Model.checkGuide("buy");
        }

        private okBuy(){
            if( this.data.byDiamond){
                if( Model.me.diamond < this.data.disPrice){
                    //ShowHint("钻石不足...");
                    BuyDiamondPanel.getInstance().show();
                }else{
                    window["__buy_prop"](this.data.id,3,1);
                }
            }else if(this.data.byCatHair && this.data.byDogHair){
                if(Model.me.myType == Model.GOOD_TYPE_CAT){
                    if( Model.me.catHair < this.data.hair){
                        ShowHint("猫毛不足...");
                    }else{
                        window["__buy_prop"](this.data.id,1,1);
                    }
                }else{
                    if( Model.me.dogHair < this.data.hair){
                        ShowHint("狗毛不足...");
                    }else{
                        window["__buy_prop"](this.data.id,2,1);
                    }
                }
            }else if(this.data.byCatHair){
                if( Model.me.catHair < this.data.hair){
                    ShowHint("猫毛不足...");
                }else{
                    window["__buy_prop"](this.data.id,1,1);
                }
            }else if(this.data.byDogHair){
                if( Model.me.dogHair < this.data.hair){
                    ShowHint("狗毛不足...");
                }else{
                    window["__buy_prop"](this.data.id,2,1);
                }
            }
        }

        public dataChanged(){
            super.dataChanged();
            this.pet_name.text = this.data.name;
            this.pet_words.text = this.data.descs;
            if( this.data.byDiamond){
                this.group_price.text = this.data.disPrice+"";
                //GetUIAssetsNum(this.data.disPrice,this.group_price,1.0,true,true);
                //this.img_diamond.visible = true;
                //this.img_money.visible = false;
                this.img_money.source = RES.getRes("img_diamond");
            }else{
                this.group_price.text = this.data.hair+"";
                //GetUIAssetsNum(this.data.hair,this.group_price,1.0,true,true);
                this.img_money.visible = true;
                if( this.data.byCatHair && this.data.byDogHair){
                    if( Model.me.myType == Model.GOOD_TYPE_CAT){
                        this.img_money.source =  RES.getRes("img_money_cat");
                    }else{
                        this.img_money.source = RES.getRes("img_money_dog");
                    }
                }else{
                    this.img_money.source = RES.getRes(this.data.byCatHair ? "img_money_cat" : "img_money_dog");
                }
                //this.img_money.commitProperties();
                //this.img_diamond.visible = false;
            }
            this.img_icon.source = this.data.icon;
        }

        public pet_name:eui.Label;
        public pet_words:eui.Label;
        public img_buy:eui.Image;
        public img_diamond:eui.Image;
        public group_price:eui.BitmapLabel;
        public img_icon:eui.Image;
        public img_money:eui.Image;
        public partAdded(name:string, instance:any) {
            super.partAdded(name, instance);
            if (name == "pet_name") {
                this.pet_name = instance;
            } else if (name == "pet_words") {
                this.pet_words = instance;
            }else if (name == "img_buy") {
                this.img_buy = instance;
                this.img_buy.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBuy, this);
            }
            //else if (name == "img_diamond") {
            //    this.img_diamond = instance;
            //}
            else if (name == "group_price") {
                this.group_price = instance;
            }else if (name == "img_icon") {
                this.img_icon = instance;
            }else if (name == "img_money") {
                this.img_money = instance;
            }
        }
    }
//}